package ipmsg.file;

import ipmsg.etc.GlobalVar;
























public class Configuration
{
  static String path;
  
  public static void readConfig()
  {
    path = GlobalVar.USER_HOME + GlobalVar.FILE_PATH_DELIMITER + ".LanMsg_ipMsg" + GlobalVar.FILE_PATH_DELIMITER + "LanMsg.conf";
    

    try
    {
      ConfigOperation config = new ConfigOperation(path);
      
      GlobalVar.USER_NAME = config.getValue("userName");
      GlobalVar.HOST_NAME = config.getValue("hostName");
      GlobalVar.SEND_MSG_STYLE = (char)new Integer(config.getValue("sendMsgStyle")).intValue();
      
      GlobalVar.CHARACTER_ENCODING = config.getValue("character_encoding");
      

      GlobalVar.THEME = (char)new Integer(config.getValue("theme")).intValue();
      
      GlobalVar.LOG_MAX_LEN = new Integer(config.getValue("logMaxLen")).intValue();
    }
    catch (NumberFormatException e) {
      errorHappen();
    } catch (ConfigurationException e) {
      errorHappen();
    }
  }
  
  public static void writeConfig()
  {
    try
    {
      ConfigOperation config = new ConfigOperation(path);
      
      config.setValue("userName", GlobalVar.USER_NAME);
      config.setValue("hostName", GlobalVar.HOST_NAME);
      config.setValue("sendMsgStyle", GlobalVar.SEND_MSG_STYLE + "");
      config.setValue("character_encoding", GlobalVar.CHARACTER_ENCODING);
      config.setValue("theme", GlobalVar.THEME + "");
      config.setValue("logMaxLen", GlobalVar.LOG_MAX_LEN + "");
      config.saveFile();
    }
    catch (ConfigurationException ex)
    {
      ex.printStackTrace();
    }
  }
  
  private static void errorHappen() {
    GlobalVar.initDefault();
    

    UtilityConfig.creatFile(GlobalVar.USER_HOME + GlobalVar.FILE_PATH_DELIMITER + ".LanMsg_ipMsg", '\001');
    




    UtilityConfig.creatFile(path, '\000');
  }
}
